﻿//
//
/////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThinMediaLibrary;
using SelectionMode = System.Windows.Controls.SelectionMode;
using UserControl = System.Windows.Controls.UserControl;

/////////////////////////////////////////////////////////////////////////////
namespace ThinControlLibrary
{
    /// <summary>
    /// Interaction logic for PlayListControl.xaml
    /// </summary>
    /////////////////////////////////////////////////////////////////////////
    public partial class PlayListControl : UserControl
    {
        /////////////////////////////////////////////////////////////////////
        public PlayListControl()
        {
            InitializeComponent();
            _init();
        }

        /////////////////////////////////////////////////////////////////////
        private void _init()
        {
            var t = Resources["playListDataSource"];
        }

        /////////////////////////////////////////////////////////////////////
        private void playMedialistView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            PlayListMediaItem item = playMedialistView.SelectedItem as PlayListMediaItem;
            PlayListMediaCollection mediaCollection = playMedialistView.ItemsSource as PlayListMediaCollection;
            if (item != null && mediaCollection != null)
            {
                MediaCommands.Play.Execute(item, Window.GetWindow(this));
            }
        }

        /////////////////////////////////////////////////////////////////////
        private void NewListButtonClick(object sender, RoutedEventArgs e)
        {
            _addMediaFiles(false);
        }

        /////////////////////////////////////////////////////////////////////
        private void AddListButtonClick(object sender, RoutedEventArgs e)
        {
            _addMediaFiles(true);
        }

        /////////////////////////////////////////////////////////////////////
        private void _addMediaFiles(bool append)
        {
            OpenFileDialog dlg = new OpenFileDialog
            {
                FileName = "",
                DefaultExt = ".mp3",
                Filter = "Media files (.mp3)|*.mp3",
                Multiselect = true
            };

            DialogResult result = dlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                PlayListMediaCollection mediaCollection = playMedialistView.ItemsSource as PlayListMediaCollection;
                if (mediaCollection != null && dlg.FileNames != null && dlg.FileNames.Length > 0)
                {
                    if (!append)
                    {
                        mediaCollection.Clear();
                    }
                    foreach (string fileName in dlg.FileNames)
                    {
                        FileInfo fi = new FileInfo(fileName);
                        mediaCollection.Add(new PlayListMediaItem() { Name = fi.Name, Path = fileName });
                    }
                }
            }
        }

        /////////////////////////////////////////////////////////////////////
        public PlayListMediaCollection PlayList
        {
            get { return playMedialistView.ItemsSource as PlayListMediaCollection; }
        }

        /////////////////////////////////////////////////////////////////////
        public PlayListMediaItem SelectPreviousTrack()
        {
            _selectPreviuosMediaItem();
            return _getSelectedMediaItem();
        }

        public PlayListMediaItem SelectNextTrack()
        {
            _selectNextMediaItem();
            return _getSelectedMediaItem();
        }

        /////////////////////////////////////////////////////////////////////
        private void _selectMediaItem(int index)
        {
            PlayListMediaCollection mediaCollection = playMedialistView.ItemsSource as PlayListMediaCollection;
            if (mediaCollection != null && index < mediaCollection.Count)
            {
                playMedialistView.SelectedIndex = index;
            }
        }

        /////////////////////////////////////////////////////////////////////
        private void _selectPreviuosMediaItem()
        {
            PlayListMediaCollection mediaCollection = playMedialistView.ItemsSource as PlayListMediaCollection;
            if (mediaCollection != null && playMedialistView.SelectedIndex > 0)
            {
                playMedialistView.SelectedIndex--;
            }
        }

        /////////////////////////////////////////////////////////////////////
        private void _selectNextMediaItem()
        {
            PlayListMediaCollection mediaCollection = playMedialistView.ItemsSource as PlayListMediaCollection;
            if (mediaCollection != null && playMedialistView.SelectedIndex + 1 < mediaCollection.Count)
            {
                playMedialistView.SelectedIndex++;
            }
        }

        /////////////////////////////////////////////////////////////////////
        private PlayListMediaItem _getSelectedMediaItem()
        {
            PlayListMediaCollection mediaCollection = playMedialistView.ItemsSource as PlayListMediaCollection;
            if (mediaCollection != null && mediaCollection.Count > 0)
            {
                int index = playMedialistView.SelectedIndex;
                return mediaCollection.Where((t, i) => i == index).FirstOrDefault();
            }
            return null;
        }

        /////////////////////////////////////////////////////////////////////
        public PlayListMediaItem GetSelectedTrack()
        {
            return _getSelectedMediaItem();
        }

        /////////////////////////////////////////////////////////////////////
        private void AddUrlButton_Click(object sender, RoutedEventArgs e)
        {
            //inputFieldTextBox.Focus();
        }

        private void inputFieldTextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            //добавить стиль тултипов

            //if (e.Key == Key.Enter)
            //{

            //    PlayListMediaCollection mediaCollection = playMedialistView.ItemsSource as PlayListMediaCollection;
            //    if (mediaCollection != null)
            //    {
            //        mediaCollection.Add(new PlayListMediaItem() { Path = inputFieldTextBox.Text, Name = inputFieldTextBox.Text });
            //        inputFieldTextBox.Text = "";
            //    }
            //}
        }

        /////////////////////////////////////////////////////////////////////
        public int TracksCount
        {
            get { return Convert.ToInt32(TracksCountLabel.Content); }
            set { TracksCountLabel.Content = value.ToString(CultureInfo.InvariantCulture); }
        }

        /////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Gets or sets the duration of the all tracks (in seconds)
        /// </summary>
        public int TracksDuarionCount
        {
            get { return 0; }
            set {  }
        }
    }
/////////////////////////////////////////////////////////////////////////////
}//ThinControlLibrary
